export class PosHelper{

  /**
   * 寻找指定房间靠近自己的某个房间
   * @param roomName 
   * @returns 
   */
  public static findClosestMyRoom(roomName: string):string {
    let nearestRoom: string = "";
    let shortestDistance: number | undefined;
    // 遍历所有房间
    for (const room of Object.values(Game.rooms)) {
      if (!room.controller || !room.controller.my) continue;
  
      const route = Game.map.findRoute(roomName, room.name);
      if (route === -2) continue;
  
      const distance = route.length;
      if (shortestDistance === undefined || distance < shortestDistance) {
        shortestDistance = distance;
        nearestRoom = room.name;
      }
    }
  
    return nearestRoom;
  }

  /**
   * 获取指定房间最靠近自己的某个房间
   * @param roomName 
   */
  public static getClosestMyRoom(roomName: string):string | undefined {
    let closestRooms:Array<{ roomName:string,myRoomName:string }> = new Array<{ roomName:string,myRoomName:string }>();
    if(Memory.closestRooms === undefined){
      Memory.closestRooms = new Array<{ roomName:string,myRoomName:string }>();
    }
    else{
      closestRooms = Memory.closestRooms.filter(room => room.roomName === roomName); // 从内存中找到此房间附近的房间
    }
    if(closestRooms.length <= 0){
      const myRoomName = this.findClosestMyRoom(roomName);
      const closestRoom = {roomName:roomName,myRoomName:myRoomName};
      closestRooms.push(closestRoom);
      Memory.closestRooms.push(closestRoom);
    }
    if(closestRooms.length > 0){
      return closestRooms[0].myRoomName;
    }
  }
}